﻿using Manon.Repository.Collections;
using Manon.Repository.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Manon.Repository
{

    /// <summary>
    /// 定义工作单元接口
    /// </summary>
    public interface IUnitOfWork : IDisposable
    {
        /// <summary>
        /// 获取DBContext
        /// </summary>
        /// <returns></returns>
        ICommonDbContext DbContext { get; }

        DbSet<TEntity> Set<TEntity>() where TEntity : class;
         
        Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default(CancellationToken));
          

        /// <summary>
        /// DbContext提交修改（异步）
        /// </summary>
        /// <returns></returns>
        Task<int> SaveChangesAsync();



        void SetEntry<TEntity>(TEntity entity, EntityState entityState) where TEntity : class;

        void SetEntry(object entity, EntityState entityState);
        #region 

        /// <summary>
        /// 执行原生sql语句
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        Task<int> ExecuteSqlCommandAsync(string sql, params object[] parameters); 

        /// <summary>
        /// 使用原生sql查询来获取指定数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        Task<IEnumerable<TEntity>> FromSqlAsync<TEntity>(string sql, params object[] parameters) where TEntity : class, new();
         
        /// <summary>
        /// 异步sql分页
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="Sql"></param>
        /// <param name="Input"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        Task<PagedList<TEntity>> GetPagedListBySqlAsync<TEntity>(string sql, BasePageInput input, params object[] parameters) where TEntity : class, new();

        #endregion

    }

}
